package com.lx.mobileplayer.mvp.view.fragment

import android.Manifest
import android.os.Bundle
import android.preference.Preference
import android.preference.PreferenceFragment
import android.preference.PreferenceScreen
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.lx.mobileplayer.R
import com.lx.permission.PermissionCallback
import com.lx.permission.PermissionUtil
import org.jetbrains.anko.defaultSharedPreferences
import org.jetbrains.anko.toast

/**
 * 类说明：设置界面里嵌套的fragment
 * Created by liuxiong on 2019/4/24.
 */
class SettingFragment: PreferenceFragment() {


    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        addPreferencesFromResource(R.xml.setting)
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        var canClear=defaultSharedPreferences.getBoolean("push",false)
        if(canClear){
            toast("可以推送通知")
        }else{
            toast("不可以推送通知")
        }
    }


    override fun onPreferenceTreeClick(preferenceScreen: PreferenceScreen?, preference: Preference?): Boolean {

//        var permission= arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
//
//        PermissionUtil.request(activity,permission,object :PermissionCallback{
//            override fun onPermissionGranted() {
//                PermissionUtil.showDialog(activity,"权限申请成功")
//                test(preference)
//            }
//
//            override fun shouldShowRational(rationalPermissons: Array<out String>?,before:Boolean) {
//                PermissionUtil.showDialog(activity,"权限申请失败")
//            }
//
//            override fun onPermissonReject(rejectPermissons: Array<out String>?) {
//                PermissionUtil.showDialog(activity,"权限申请失败并且勾选不再询问")
//            }
//        })

        test(preference)
        return true

    }

    private fun test(preference: Preference?) {
        var key = preference?.key
        when (key) {
            "clear_cache" -> Toast.makeText(activity.application, "清除缓存", Toast.LENGTH_SHORT).show()
            "about" -> Toast.makeText(activity.application, "关于", Toast.LENGTH_SHORT).show()
        }
    }
}